// 2025/10/22
// 二叉搜索树的最小绝对差

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int getMinimumDifference(TreeNode* root) {
        stack<TreeNode*> st;
        TreeNode* cur = root;
        while(cur->left)
        {
            st.emplace(cur);
            cur = cur->left;
        }
        int prev = cur->val, ans = INT_MAX;
        if(cur->right)
        {
            cur = cur->right;
            while(cur)
            {
                st.emplace(cur);
                cur = cur->left;
            }
        }
        while(!st.empty())
        {
            cur = st.top(); st.pop();
            ans = min(ans, cur->val - prev);
            prev = cur->val;
            if(cur->right)
            {
                cur = cur->right;
                while(cur)
                {
                    st.emplace(cur);
                    cur = cur->left;
                }
            }
        }
        return ans;
    }
};